package com.lm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lm.controller.UploadController;
import com.lm.mapper.*;
import com.lm.pojo.*;
import com.lm.service.DocterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @dscription:TODO
 * @author32477
 * @date 2025/9/21 21:12
 */
@Service
public class DocterServiceImpl implements DocterService {
    @Autowired
    DocterMapper docterMapper;
    @Autowired
    UploadController uploadController;
    @Autowired
    OrgMapper orgMapper;
    @Autowired
    TeamManageMapper teamManageMapper;
    @Autowired
    DocterTagMapper docterTagMapper;
    @Autowired
    DocterAndTeamMapper docterAndTeamMapper;



    @Override
    public List<Docter> findDocters() {
        List<Docter> list = docterMapper.selectList(null);
        return list;
    }
    @Override
    public List<DocterTag> findTag() {
        return docterTagMapper.selectList(null);
    }

    @Override
    public IPage<Docter> findList(Docter docter) {
        IPage<Docter>page = new Page<>(docter.getPageNo(),docter.getPageSize());
        IPage<Docter>pageInfo = docterMapper.findList(page,docter);
        return pageInfo;
    }
    @Override
    @Transactional
    public void addDocter(Docter docter) {
        docter.setDocterPassword("111111");
        docter.setDocterRoleId(0);//默认权限0
        docterMapper.insert(docter);
        //添加到队伍和医生关系表
        DocterAndTeam docterAndTeam = new DocterAndTeam();
        docterAndTeam.setDocterTeamId(docter.getDocterTeamId());
        docterAndTeam.setDocterId(docter.getDocterId());
        if (docterAndTeam.getDocterTeamId() != null) {
            docterAndTeamMapper.insert(docterAndTeam);
        }


    }
    @Override
    public void deleteDocter(Integer docterId) {
        //删除阿里云 图片
        //1.查询医生信息
        Docter docter = docterMapper.selectById(docterId);
        if (docter.getDocterAvator() != null) {
            //删除图片
            uploadController.deleteImg(docter.getDocterAvator());
        }
        //删除医生领导的团队
        docterMapper.deleteTeam(docterId);
        //删除医生与队伍关系表
        QueryWrapper<DocterAndTeam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("docter_id",docterId);
        docterAndTeamMapper.delete(queryWrapper);
        docterMapper.deleteById(docterId);


    }
    @Override
    public void updateDocter(Docter docter) {
        //删除医生关联表
        QueryWrapper<DocterAndTeam> docterAndTeamQueryWrapper = new QueryWrapper<>();
        docterAndTeamQueryWrapper.eq("docter_id",docter.getDocterId());
        docterAndTeamMapper.delete(docterAndTeamQueryWrapper);
        docterMapper.updateById(docter);
        DocterAndTeam docterAndTeam = new DocterAndTeam();
        docterAndTeam.setDocterTeamId(docter.getDocterTeamId());
        docterAndTeam.setDocterId(docter.getDocterId());
        docterAndTeamMapper.insert(docterAndTeam);
    }
    @Override
    public List<Organization> findDocterList() {
        return orgMapper.selectList(null);
    }

    @Override
    public List<TeamManage> findTeamList() {
        return teamManageMapper.selectList(null);
    }

    @Override
    public Integer findLastNumber() {
        return docterMapper.findLastNumber();
    }
}
